package com.datawin.taotaocheche.util;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.widget.Toast;

import com.datawin.taotaocheche.bean.ClueInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ExcelUtil {
    //内存地址
    public static String root = Environment.getExternalStorageDirectory()
            .getPath();
    private static String[] StrInfo = {"不限", "有", "没有"};

    public static void writeExcel(Context context, List<ClueInfo> clueInfos,
                                  String fileName) {
        try {
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && getAvailableStorage() > 1000000) {
                Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show();
                return;
            }
            String[] title = {"车系品牌", "姓名", "联系方式", "城市", "车型", "车辆年限", "里程数(公里)",  "来源", "更新时间"};
            File file;
            File dir = new File(context.getExternalFilesDir(null).getPath());
            file = new File(dir, fileName + ".xls");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 创建Excel工作表
            WritableWorkbook wwb;
            OutputStream os = new FileOutputStream(file);
            wwb = Workbook.createWorkbook(os);
            // 添加第一个工作表并设置第一个Sheet的名字
            WritableSheet sheet = wwb.createSheet("订单", 0);
            Label label;
            for (int i = 0; i < title.length; i++) {
                // Label(x,y,z) 代表单元格的第x+1列，第y+1行, 内容z
                // 在Label对象的子对象中指明单元格的位置和内容
                label = new Label(i, 0, title[i], getHeader());
                // 将定义好的单元格添加到工作表中
                sheet.addCell(label);
            }

            for (int i = 0; i < clueInfos.size(); i++) {
                ClueInfo order = clueInfos.get(i);
                Label money = new Label(0, i + 1, order.getTrainingType() + "");
                Label name = new Label(1, i + 1, order.getName() + "");
                Label mobile = new Label(2, i + 1, order.getMobile() + "");
                Label city = new Label(3, i + 1, order.getCity() + "");
                Label inCome = new Label(4, i + 1, order.getShouru() + "");
                Label job = new Label(5, i + 1, order.getAge() + "");
                Label mouth = new Label(6, i + 1, order.getZhiye() + "");
                Label source = new Label(7, i + 1, order.getSource() + "");
                Label time = new Label(8, i + 1, order.getUpdateTime() + "");
                sheet.addCell(money);
                sheet.addCell(name);
                sheet.addCell(mobile);
                sheet.addCell(city);
                sheet.addCell(inCome);
                sheet.addCell(job);
                sheet.addCell(mouth);
                sheet.addCell(source);
                sheet.addCell(time);
            }
            // 写入数据
            wwb.write();
            // 关闭文件
            wwb.close();
            Toast.makeText(context, "保存数据成功，请到手机内存卡中查看", Toast.LENGTH_LONG).show();
            ShareUtils.sendFileByOtherApp(context, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static WritableCellFormat getHeader() {
        WritableFont font = new WritableFont(WritableFont.TIMES, 10,
                WritableFont.BOLD);// 定义字体
        try {
            font.setColour(Colour.BLUE);// 蓝色字体
        } catch (WriteException e1) {
            e1.printStackTrace();
        }
        WritableCellFormat format = new WritableCellFormat(font);
        try {
            format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中
            format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中
            // format.setBorder(Border.ALL, BorderLineStyle.THIN,
            // Colour.BLACK);// 黑色边框
            // format.setBackground(Colour.YELLOW);// 黄色背景
        } catch (WriteException e) {
            e.printStackTrace();
        }
        return format;
    }

    /**
     * 获取SD可用容量
     */
    private static long getAvailableStorage() {

        StatFs statFs = new StatFs(root);
        long blockSize = statFs.getBlockSize();
        long availableBlocks = statFs.getAvailableBlocks();
        long availableSize = blockSize * availableBlocks;
        // Formatter.formatFileSize(context, availableSize);
        return availableSize;
    }
}
